#coding=utf-8

from  datetime  import  *
import random, time, ssl, cookielib, urllib2, urllib, re, os, xlwt
from urllib import urlencode
from config import _webJD, _webTB, _searchKey, _maxSearch, excel_curline
from PIL import Image

def get_xls_name():
    if not os.path.exists("result.xls"):
        return "result.xls"
    i = 1
    func_name = lambda arg: "result" + '%02d'%arg + '.xls'
    while os.path.exists(func_name(i)):
        i += 1
    return func_name(i)
    
def write_excel_header(sheet):
	sheet.write(0, 0, "关键字")
	sheet.write(0, 1, "平台")
	sheet.write(0, 2, "名称")
	sheet.write(0, 3, "销量")
	sheet.write(0, 4, "评论数")
	sheet.write(0, 5, "商品链接")
	sheet.write(0, 6, "图片")
        
def SearchTBItem(sheet, opener, item):
    global excel_curline
    
    req = urllib2.Request(_webTB + "&q=" + item)
    conn = urllib2.urlopen(req)
    text = conn.read()
    #print(text)
    
    #op = opener.open(req)
    #text = op.read()

    print("TB开始找寻：")
    print(item)

    find_titles = re.compile(',"raw_title":"(.*?)","pic_url":"').findall(text)
    find_urls = re.compile(',"detail_url":"(.*?)","view_price":"').findall(text)
    find_prices = re.compile(',"view_price":"(.*?)","view_fee":"').findall(text)
    find_pictures =  re.compile(',"pic_url":"(.*?)","detail_url":"').findall(text)
    find_commits = re.compile(',"view_sales":"(.*?)","comment_count":"').findall(text)

    indexCount = _maxSearch
    if (len(find_titles) < _maxSearch):
        indexCount = len(find_titles)
	
    if (indexCount == 0):
        print("Failed!!! -----------")
        return False
    print("Succeed!!! -----------")
    for index in range(0, indexCount):
        #print(find_titles[index])
        print("标题:" + find_titles[index])
        print("网址:" + find_urls[index])
        print("价格:" + find_prices[index] + " 元")
        #print("图片:" + "http:" + find_pictures[index].decode('unicode_escape'))
        print("销售:" + find_commits[index])
        print("-----------------------------------------------")
        print("\n")
		
        product_url = "http:" + find_urls[index]
        pic_url = "http:" + find_pictures[index]
        sheet.write(excel_curline, 0 , item)
        sheet.write(excel_curline, 1 , u"淘宝")
        sheet.write(excel_curline, 2 , find_titles[index])
        sheet.write(excel_curline, 3 , find_commits[index])
        sheet.write(excel_curline, 4 , 0)
        sheet.write(excel_curline, 5 , product_url.decode('unicode_escape'))
        #sheet.write(excel_curline, 6 , pic_url)
        download_file("./cache/tb", pic_url)
        img = Image.open("./cache/tb")
        img.load()
        imgsplit = img.split()
        if(len(imgsplit) >= 3):
            r = imgsplit[0]
            g = imgsplit[1]
            b = imgsplit[2]
            img = Image.merge("RGB", (r, g, b))
            img = img.resize((100,100), Image.ANTIALIAS)
            img.save('./cache/tb.bmp')
            sheet.insert_bitmap("./cache/tb.bmp", excel_curline, 6)
        
        excel_curline = excel_curline + 1
    return True
    
def SearchDJItem(sheet, opener, item):
    global excel_curline
    op = opener.open(_webJD + "&keyword=" + item)
    text = op.read()
    
    print("DJ开始找寻：")
    print(item)

    find_titles = re.compile('<div class="p-img">[\r\n\t]*<a target="_blank" title="(.*)" href="').findall(text)
    find_urls = re.compile('<div class="p-img">[\r\n\t]*<a target="_blank" title=".* href="//item.jd.com/([0-9]+).html" onclick=').findall(text)
    find_prices = re.compile('data-price="([0-9]+.00)"><em>¥</em><i>').findall(text)
    find_pictures = re.compile('[src|img]="(\/\/img.*.jpg)"').findall(text)
    find_commits = re.compile('<div class="p-commit"><strong>.*">([0-9]+)<\/a>').findall(text)

    indexCount = _maxSearch
    if (len(find_titles) < _maxSearch):
        indexCount = len(find_titles)
	
    for index in range(0, indexCount):
        print("标题:" + find_titles[index])
        print("网址:" + "http://item.jd.com/" + find_urls[index] + ".html")
        print("价格:" + find_prices[index] + " 元")
        print("图片:" + "http:" + find_pictures[index].decode('unicode_escape'))
        print("评论:" + find_commits[index])
        print("-----------------------------------------------")
        print("\n")
    	
        product_url = "http://item.jd.com/" + find_urls[index] + ".html"
        pic_url = "http:" + find_pictures[index]
        sheet.write(excel_curline, 0 , item)
        sheet.write(excel_curline, 1 , u"京东")
        sheet.write(excel_curline, 2 , find_titles[index])
        sheet.write(excel_curline, 3 , 0)
        sheet.write(excel_curline, 4 , find_commits[index])
        sheet.write(excel_curline, 5 , product_url.decode('unicode_escape'))
        #sheet.write(excel_curline, 6 , pic_url)
        download_file("./cache/jd", pic_url)
        img = Image.open("./cache/jd")
        img.load()
        imgsplit = img.split()
        if(len(imgsplit) >= 3):
            r = imgsplit[0]
            g = imgsplit[1]
            b = imgsplit[2]
            img = Image.merge("RGB", (r, g, b))
            img = img.resize((100,100),Image.ANTIALIAS)
            img.save('./cache/jd.bmp')
            sheet.insert_bitmap("./cache/jd.bmp", excel_curline, 6)
        
        excel_curline = excel_curline + 1
def GetRandomAgent():
    return 'Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.' + str(random.randint(1, 9527))
    
def FuckTaobaoShell(sheet, keys):
    randAgent = GetRandomAgent()
    cookietxt =  datetime.strftime(date.today(),'./cache/%Y-%m-%d_cookie.txt')
    
    ####TODO
    #delete if exist
    if os.path.exists(cookietxt):
        os.remove(cookietxt)

    #若cookie不存在，则创建
    print("检查是否存在cookie...")
    if not os.path.exists(cookietxt):
        print("啦啦啦，cookie需要重新生成啦！")
        #声明一个MozillaCookieJar对象实例来保存cookie，之后写入文件
        cookie = cookielib.MozillaCookieJar(cookietxt)
        post_data = urllib.urlencode(    
        {
         'TPL_username':"13581710227",
         'TPL_password':"124272113w",
         })
      
        path = 'https://login.taobao.com/member/login.jhtml'    
        opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie))    
     
        opener.addheaders = [('User-agent', randAgent)]     
        
        urllib2.install_opener(opener)    
        req = urllib2.Request(path,post_data)
        conn = urllib2.urlopen(req)
        html = conn.read().decode('gbk','ignore')

        #保存cookie到文件
        cookie.save(ignore_discard=True, ignore_expires=True)

    print("cookie检查完毕!")
    #读取cookie，访问网站
    cookie = cookielib.MozillaCookieJar()
    cookie.load(cookietxt, ignore_discard=True, ignore_expires=True)
    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie))
    opener.addheaders = [('User-agent', randAgent)]     
    urllib2.install_opener(opener) 

    index = 1
    for element in keys:
        if not SearchTBItem(sheet, opener, element):
            SearchTBItem(sheet, opener, element)
        time.sleep(5)
        print("time:", index)
        index = index + 1

def FuckTaobao(sheet):
    EVERY_HOW = 20
    global _searchKey
    if len(_searchKey) <= EVERY_HOW:
        FuckTaobaoShell(sheet, _searchKey)
        return

    tmplen = len(_searchKey)/EVERY_HOW
    index = 1
    while index <= tmplen:
        FuckTaobaoShell(sheet, _searchKey[(index-1)*EVERY_HOW:EVERY_HOW*index])
        index = index + 1
    
    if (len(_searchKey) - tmplen*EVERY_HOW) > 0:
        FuckTaobaoShell(sheet, _searchKey[tmplen*EVERY_HOW:len(_searchKey)])


def FuckJingdong(sheet):
    global _searchKey
    cj = cookielib.CookieJar()
    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
    opener.addheaders=[('User-agent','Mozilla/5.0 (Windows NT 6.1; rv:17.0) Gecko/20100101 Firefox/17.0')]
    
    index = 1
    for element in _searchKey:
        SearchDJItem(sheet, opener, element)
        print("time:", index)
        index = index + 1

def download_file(dstfile, url):
    try:
        cj=cookielib.LWPCookieJar()
        opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
        urllib2.install_opener(opener)
        
        req=urllib2.Request(url)
        operate=opener.open(req)
        data=operate.read()
        
        #delete if exist
        if os.path.exists(dstfile):
            os.remove(dstfile)
        
        #save to dstfile
        pic_file = open(dstfile, "wb")
        pic_file.write(data)
        pic_file.flush()
        pic_file.close()
    except BaseException, e:
        print e
        
def main():
    global _maxSearch
    global _searchKey
    global excel_curline
    
    #read keywords
    _searchKey = []
    rfile = open("./cache/keywords.txt", 'r')
    line = rfile.readline()
    while line:
        line = line.replace(" ", "")
        line = line.replace("\t", "")
        line = line.replace("\r", "")
        line = line.replace("\n", "")
        _searchKey.append(line)
        line = rfile.readline()
    rfile.close()
    if len(_searchKey) == 0:
        print("请设置至少一个搜索字!")
        return
    
    #read top number
    _maxSearch = 1
    rfile = open("./cache/top.txt", 'r')
    line = rfile.readline()
    if line:
        _maxSearch = int(line)
    rfile.close()
    if(_maxSearch <=0 or _maxSearch > 100):
        _maxSearch = 1
    print("Deep:")
    print(_maxSearch)
    
    #解决xp机器上出来的ssl错误
    ssl._create_default_https_context = ssl._create_unverified_context
    
    #从excel第0行开始计数
    excel_curline = 0
    
    xls_file = xlwt.Workbook(encoding='utf-8')
    sheet= xls_file.add_sheet('Sheet1',cell_overwrite_ok=True)
    write_excel_header(sheet)
    excel_curline = excel_curline + 1
    
    print("-------------------TaoBao-----------------------------")
    FuckTaobao(sheet)
    
    print("---------------------JingDong-------------------------")
    FuckJingdong(sheet)
    
    xlsfilename = get_xls_name()
    xls_file.save(xlsfilename)
    os.startfile(xlsfilename)
    
main();
